<template>
    <div class="love-container">
        <button @click="getLove">获取一句情话</button>
        <ul>
            <li v-for="item in loveStore.loveList" :key="item.id">
                {{ item.msg }}
            </li>
        </ul>
    </div>
</template>

<script setup>
import axios from 'axios';
import { nanoid } from 'nanoid'; // 生成唯一的id
import { useLoveStore } from '../store/love';

const loveStore = useLoveStore()
loveStore.$subscribe((mutation, state) => {
    console.log('loveStore里的数据发生了变化');
    console.log('mutation', mutation);
    console.log('state', state);
    localStorage.setItem('loveList', JSON.stringify(state.loveList))
})

function getLove() {
    loveStore.getLove()
}

// async function getLove() {
//     // 下面这行写法是：连续解构赋值+重命名（content重命名为title）
//    let {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
//    loveList.push({id: nanoid(), msg: title})
// }
</script>

<style scoped>
.love-container {
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
}

button {
    background-color: #ff6b81;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.3s ease;
}

button:hover {
    background-color: #ff4757;
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(255, 107, 129, 0.3);
}

ul {
    list-style: none;
    padding: 0;
    margin-top: 20px;
}

li {
    background-color: #fff;
    padding: 15px;
    margin: 10px 0;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
    color: #333;
    font-size: 16px;
}

li:hover {
    transform: translateX(5px);
    background-color: #fff5f6;
}
</style>